/**
 * 
 */
package co.droidforum.foodx.ws;

import java.io.IOException;

import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;

/**
 * @author andres.alzate
 *
 */
public class RemoteResponseErrorHandler extends DefaultResponseErrorHandler {

	@Override
	public void handleError(ClientHttpResponse response) throws IOException {
		HttpStatus httpStatus = response.getStatusCode();
		if(httpStatus == HttpStatus.PRECONDITION_FAILED){
			String msgRemote = response.getHeaders().get(ServiceResources.STATUS_CODE_MTS).get(0);
			throw new RemoteBusinessServiceException(msgRemote);
		}
		
		try {
			super.handleError(response);
		} catch (Exception e) {
			throw new RemoteConnectionServiceException(e);
		}
	}

	@Override
	public boolean hasError(ClientHttpResponse response) throws IOException {
		return super.hasError(response);
	}
}